/**
 * 区域设置 编辑页
 *
 * 作者 admin
 * 日期 2024-07-31 14:35:47
 */
layui.use(['jquery','febs', 'form', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#baseArea-edit'),
        $location = $view.find('#location'),
        $areaType = $view.find('#areaType',)
        validate = layui.validate;

    initDictItemSelect_areaList();
    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
        if (baseAreaData) {
            form.val("baseArea-edit-form", {
                "areaId": baseAreaData.areaId,
                "tenantId": baseAreaData.tenantId,
                "areaType": baseAreaData.areaType,
                "areaName": baseAreaData.areaName,
                "areaCoordinate": baseAreaData.areaCoordinate,
                "sort": baseAreaData.sort,
                "remark": baseAreaData.remark,
            });
            if(baseAreaData.status === '1'){
                $view.find('input[type=checkbox]').attr("checked","true");
                form.render('checkbox');
            }
            if(baseAreaData.areaCoordinate == '' || baseAreaData.areaCoordinate == null){
                $("#location").html('<span style="color:#ff5652;cursor:pointer">未设置</span>');
            }else{
                $("#location").html('<span style="color:#01aaed;cursor:pointer">已设置</span>');
            }

        } else {
            $("#location").html('<em class="layui-icon">&#xe7a7;</em>新增');
            form.val("baseArea-edit-form", {
                "tenantId": POPUP_DATA.tenantId
            });
        }
    }

    function initDictItemSelect_areaList(){
        // 项目类型
        febs.initDictItemSelect($areaType,'SAFETY_AREA_TYPE', baseAreaData.areaType,null);
    }

    form.on('submit(baseArea-edit-form-submit)', function (data) {
        if(data.field.status == "on"){
            data.field.status = '1';
        }else{
            data.field.status = '0';
        }
        // console.log("保存信息"+JSON.stringify(data.field));
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        febs.modal.confirm('保存提示', '是否保存区域信息？', function () {
            if (data.areaId && data.areaId.length > 0) {
                febs.postArray(ctx + 'baseArea/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-baseArea').find('#query').trigger('click');
                    febs.reloadPage();

                });
            } else {
                febs.postArray(ctx + 'baseArea', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-baseArea').find('#query').trigger('click');
                });
            }
        });
    }

    $location.on('click', function () {
        let areaId = "areaId";
        if(baseAreaData != null){
            areaId = baseAreaData.areaId;
        }
        febs.modal.open('区域地图', 'baseArea/baseAreaEditMap/'+areaId, {
            btn: ['提交','取消'],
            area: [$(window).width() <= 750 ? '95%' : '95%', '95%'],
            yes: function (index, layero) {
                layer.close(index);
                console.log('所有点位='+ localStorage.getItem("newPoints"));
                let pointsArr = localStorage.getItem("newPoints");
                $('input[name="areaCoordinate"]').val(pointsArr);
                localStorage.removeItem("newPoints");
            },
            data:{
            }
        });
    });
});